class Solution {
public:
    int n, m;
    vector<int> w;
    int ans = INT_MAX;
    int res = 1;

    int calc() {
        int tot = 0;
        for (int i = 0; i < n; i ++ ) {
            tot += w[i];
        }
        if (tot > 0) {
            ans = min(ans, res);
        }
        return tot;
    }

    void simulate_anneal() {
        random_shuffle(w.begin(), w.end());
        res = 0;
        for (double t = 1e6; t > 1e-5; t *= 0.97) {
            int a = rand() % n, b = rand() % n;
            int x = calc();
            swap(w[a], w[b]);
            res++;
            int y = calc();
            int delta = x - y;
            if (!(exp(-delta / t) > (double)rand() / RAND_MAX))
            {
                swap(w[a], w[b]);
                res++;
            }
        }
    }


    int magicTower(vector<int>& nums) {
        int total = 0;
        for (int i = 0; i < nums.size(); i++) {
            total += nums[i];
        }
        if (total < 1) return -1;
        w = nums;
        for (int i = 0; i < 80; i ++ ) simulate_anneal();
        return ans;
    }
};